package com.apply.leetcode.puzzle203;

public class Sum {

    public static int sum(int[] arr) {
        return sum(arr, 0);
    }

    /**
     * 真正的递归函数
     * 计算 arr[leftMargin...n]）区间内数的和
     * @param arr
     * @param leftMargin
     * @return
     */
    private static int sum(int[] arr, int leftMargin) {
        if (leftMargin == arr.length) {
            return 0;
        }
        return arr[leftMargin] + sum(arr, leftMargin + 1);
    }

    public static void main(String[] args) {
        int[] nums = {1,2,3,4,5,6,7,8};
        System.out.println(sum(nums));
    }

}
